package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.mscyb.bean.JqbjfxPartsrepair;
import com.jy.adscm.operation.mscyb.bean.JqbjfxPartsrepair;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbjfxPartsrepairMapper;
import com.jy.adscm.operation.mscyb.service.JqbjfxPartsrepairService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-10
 */
@Service
public class JqbjfxPartsrepairServiceImpl extends ServiceImpl<JqbjfxPartsrepairMapper, JqbjfxPartsrepair> implements JqbjfxPartsrepairService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Override
    public boolean insertJqbjfxPartsrepair(JqbjfxPartsrepair jqbjfxPartsrepair) {
        //根据部件id查询部件名

        if (AdscmBaseUtils.getInstance().isNotEmpty(jqbjfxPartsrepair.getResourcename(),null)){
            JyCode jyCode = jyCodeMapper.selectById(jqbjfxPartsrepair.getResourcename());
            String partcode = "F"+jyCode.getCode();
            //查询目前最大的部件编码
            String yearMonth = new SimpleDateFormat("yyMM").format(new Date());
            Wrapper<JqbjfxPartsrepair> wrapper=new EntityWrapper<>();
            wrapper.like("TASKCODE", partcode + yearMonth, SqlLike.RIGHT);
            wrapper.orderBy("TASKCODE",false);
            List<JqbjfxPartsrepair> jqbjfxPartsrepairs = super.selectList(wrapper);
            String code ="";
            if (jqbjfxPartsrepairs.size()>0){
                JqbjfxPartsrepair oldjqbjfxPartsrepair = jqbjfxPartsrepairs.get(0);
                String oldCode = oldjqbjfxPartsrepair.getTaskcode();
                Integer temp = Integer.valueOf(oldCode.substring(oldCode.length()-2, oldCode.length())) ;
                temp=temp+1;
                String nums = new DecimalFormat("00").format(temp);
                code=partcode+yearMonth+nums;
            }else {
                code=partcode+yearMonth+"00";
            }
            code.trim();
            jqbjfxPartsrepair.setTaskcode(code);
        }
        boolean insert = super.insert(jqbjfxPartsrepair);
        return insert;
    }
      

    @Override
    public Boolean updateJqbjfxPartsrepair(JqbjfxPartsrepair jqbjfxPartsrepair, Token token) {
        //待办修改
        if (jqbjfxPartsrepair.getTaskstate()==0 && jqbjfxPartsrepair.getTaskfinishstate()==0 &&
                "0".equals(jqbjfxPartsrepair.getIstaskstart()) &&  "0".equals(jqbjfxPartsrepair.getIstaskbegin()) &&
                "0".equals(jqbjfxPartsrepair.getIstaskend())  && "0".equals(jqbjfxPartsrepair.getIsreviewtask())
        ){

            return  super.updateById(jqbjfxPartsrepair);
        }
        //接收任务
        if (jqbjfxPartsrepair.getTaskstate()==0 && jqbjfxPartsrepair.getTaskfinishstate()==0 &&
                "1".equals(jqbjfxPartsrepair.getIstaskstart()) &&  "0".equals(jqbjfxPartsrepair.getIstaskbegin()) &&
                "0".equals(jqbjfxPartsrepair.getIstaskend())  && "0".equals(jqbjfxPartsrepair.getIsreviewtask())
        ){
            jqbjfxPartsrepair.setIstaskstart("1");
            jqbjfxPartsrepair.setTaskstartaddtime(new Date());
            jqbjfxPartsrepair.setTaskstate(1);
            return  super.updateById(jqbjfxPartsrepair);
        }
        //开始任务
        if (jqbjfxPartsrepair.getTaskstate()==1 && jqbjfxPartsrepair.getTaskfinishstate()==0 &&
                "1".equals(jqbjfxPartsrepair.getIstaskstart()) &&  "1".equals(jqbjfxPartsrepair.getIstaskbegin()) &&
                "0".equals(jqbjfxPartsrepair.getIstaskend())  && "0".equals(jqbjfxPartsrepair.getIsreviewtask())
        ){
            jqbjfxPartsrepair.setIstaskbegin("1");
            jqbjfxPartsrepair.setTaskbeginaddtime(new Date());
            return super.updateById(jqbjfxPartsrepair);
        }
        //结束任务
        if (jqbjfxPartsrepair.getTaskstate()==1 && jqbjfxPartsrepair.getTaskfinishstate()==0 &&
                "1".equals(jqbjfxPartsrepair.getIstaskstart()) &&  "1".equals(jqbjfxPartsrepair.getIstaskbegin()) &&
                "1".equals(jqbjfxPartsrepair.getIstaskend())  && "0".equals(jqbjfxPartsrepair.getIsreviewtask())
        ){
            jqbjfxPartsrepair.setIstaskend("1");
            jqbjfxPartsrepair.setTaskendaddtime(new Date());
            return super.updateById(jqbjfxPartsrepair);
        }
        //复核任务
        if (jqbjfxPartsrepair.getTaskstate()==1 && jqbjfxPartsrepair.getTaskfinishstate()==1 &&
                "1".equals(jqbjfxPartsrepair.getIstaskstart()) &&  "1".equals(jqbjfxPartsrepair.getIstaskbegin()) &&
                "1".equals(jqbjfxPartsrepair.getIstaskend())  && "1".equals(jqbjfxPartsrepair.getIsreviewtask())
        ){
            jqbjfxPartsrepair.setIsreviewtask("1");
            jqbjfxPartsrepair.setReviewaddtime(new Date());
            jqbjfxPartsrepair.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqbjfxPartsrepair);
        }

        return null;
    }

    @Override
    public List<JqbjfxPartsrepair> selectJqbjfxPartsrepair(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<JqbjfxPartsrepair> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<JqbjfxPartsrepair> jqbjfxPartsrepairs = super.selectList(wrapper);
        for (JqbjfxPartsrepair jqbjfxPartsrepair : jqbjfxPartsrepairs) {
            if ( jqbjfxPartsrepair.getTaskreleaseperson()!=null && !"".equals( jqbjfxPartsrepair.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqbjfxPartsrepair.getTaskreleaseperson());
                jqbjfxPartsrepair.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqbjfxPartsrepair.getTaskperformperson()!=null && !"".equals( jqbjfxPartsrepair.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqbjfxPartsrepair.getTaskperformperson());
                jqbjfxPartsrepair.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqbjfxPartsrepair.getReviewperson()!=null && !"".equals( jqbjfxPartsrepair.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqbjfxPartsrepair.getReviewperson());
                jqbjfxPartsrepair.setReviewpersonName(jyUser.getName());
            }
            if ( jqbjfxPartsrepair.getTasknums()!=null && !"".equals( jqbjfxPartsrepair.getTasknums())){
                JyUser jyUser = jyUserMapper.selectById(jqbjfxPartsrepair.getTasknums());
                jqbjfxPartsrepair.setTasknumsName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbjfxPartsrepair.getResourcename(),null)){
                JyCode jyCode = jyCodeMapper.selectById(jqbjfxPartsrepair.getResourcename());
                jqbjfxPartsrepair.setResourcenameName(jyCode.getName());
            }


        }

        return jqbjfxPartsrepairs;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("100");
        return jyUsers;
    }
}
